package cn.edu.sjtu.se.dslab.rhapsody.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class BusAgent extends Agent implements Serializable{
	
	private static final long serialVersionUID = -559948263470808266L;
	
	private String busZone;
	private List<String> entityZone;
	private String outerSystem;
	private int frequency;
	
	private Event readInitEvent;
	private Event writeInitEvent;
	private Event readProcessingEvent;
	private Event writeProcessingEvent;
	
	public BusAgent(){
		type = AGENT_BUS;
		name = "undefined";
		entityZone = new ArrayList<String>();
	}


	public String getBusZone() {
		return busZone;
	}

	public void setBusZone(String busZone) {
		this.busZone = busZone;
	}
	
	

	public List<String> getEntityZone() {
		return entityZone;
	}


	public void setEntityZone(List<String> entityZone) {
		this.entityZone = entityZone;
	}


	public String getOuterSystem() {
		return outerSystem;
	}

	public void setOuterSystem(String outerSystem) {
		this.outerSystem = outerSystem;
	}

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int frequency) {
		this.frequency = frequency;
	}


	public Event getReadInitEvent() {
		return readInitEvent;
	}


	public void setReadInitEvent(Event readInitEvent) {
		this.readInitEvent = readInitEvent;
	}

	


	public Event getWriteInitEvent() {
		return writeInitEvent;
	}


	public void setWriteInitEvent(Event writeInitEvent) {
		this.writeInitEvent = writeInitEvent;
	}


	public Event getReadProcessingEvent() {
		return readProcessingEvent;
	}


	public void setReadProcessingEvent(Event readProcessingEvent) {
		this.readProcessingEvent = readProcessingEvent;
	}


	public Event getWriteProcessingEvent() {
		return writeProcessingEvent;
	}


	public void setWriteProcessingEvent(Event writeProcessingEvent) {
		this.writeProcessingEvent = writeProcessingEvent;
	}


	@Override
	public String getAgentInitMethod() {
		StringBuilder sb = new StringBuilder();
//		if (null != readInitEvent){
//			sb.append(readInitEvent.getName()).append("();\n");
//		}
//		if (null != writeInitEvent){
//			sb.append(writeInitEvent.getName()).append("();");
//		}
		sb.append("SF").append(name).append("Init(rate);");
		return sb.toString();
	}


	@Override
	public String getAgentDelegateMethod() {
		StringBuilder sb = new StringBuilder();
		sb.append("SF").append(name).append("Delegate();");
		return sb.toString();
	}


	@Override
	public String getAgentInitMethodDeclaration() {
		StringBuilder sb = new StringBuilder("SFInjection SFCode SF");
		sb.append(name).append("Init(SFClockRate rate){\n");
		for (String ez: entityZone){
			sb.append("\t").append(ez).append("\n");
		}
		if (null != readInitEvent){
			sb.append("\t").append(readInitEvent.getEventMethodName()).append("\n");
		}
		if (null != writeInitEvent){
			sb.append("\t").append(writeInitEvent.getEventMethodName()).append("\n");
		}
		sb.append("return SFCodeOK; \n}");
		return sb.toString();
	}

	@Override
	public String getAgentDelegateMethodDeclaration() {
		StringBuilder sb = new StringBuilder("SFInjection SFCode SF");
		sb.append(name).append("Delegate(){\n");
		if (null != readProcessingEvent){
			sb.append("\t").append(readProcessingEvent.getEventMethodName()).append("\n");
		}
		if (null != writeProcessingEvent){
			sb.append("\t").append(writeProcessingEvent.getEventMethodName()).append("\n");
		}
		sb.append("return SFCodeOK; \n}");

		return sb.toString();
	}
	
	
	
}
